package com.hsy.linemoney.modules.main.actionbar;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.deparse.dpframe.tools.utils.DPGsonUtil;
import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.common.Config;
import com.hsy.linemoney.apiservice.LineMoneyContext;
import com.hsy.linemoney.modules.main.MainActivity;

/**
 * Created by Administrator on 2016/8/30.
 */
public class HomeActionBar extends RelativeLayout implements IActionBar {
    protected View rootView;
    protected TextView text;
    protected TextView homeRight;
    private MainActivity activity;

    public HomeActionBar(Context context) {
        super(context);
        init(context);
    }

    public HomeActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HomeActionBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @Override
    public void setActivity(MainActivity activity) {
        this.activity = activity;
    }


    private void init(Context context) {
        addView(inflate(context, R.layout.actionbar_home, null), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        initView(this, context);
        final String[] items = Config.getAvailableLangNameArray();
        if (2 == items.length) {
            if (items[0].equals(LineMoneyContext.getLocalLanguagePair().name)) {
                homeRight.setText(items[1]);
            } else {
                homeRight.setText(items[0]);
            }
        }
    }

    private void initView(View rootView, final Context context) {
        text = (TextView) rootView.findViewById(R.id.text);
        homeRight = (TextView) rootView.findViewById(R.id.home_right);
        homeRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] items = Config.getAvailableLangNameArray();
//                DPLogUtil.d("hhh " + DPGsonUtil.toJson(items) + "---" + LineMoneyContext.getLocalLanguagePair().toString());
                if (items.length > 2) {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(activity).setItems(items, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            LineMoneyContext.clearData();

                            LineMoneyContext.setLocalLanguagePair(Config.getAvailableLang(which));
                            dialog.dismiss();
                            activity.startActivityWithTaskCleared(MainActivity.class);
                        }
                    });
                    dialog.show();
                } else if (items.length > 1) {
                    DPLogUtil.d("items[0]=" + items[0] + ", localLang=" + LineMoneyContext.getLocalLanguagePair().toString());
                    LineMoneyContext.clearData();

                    if (items[0].equals(LineMoneyContext.getLocalLanguagePair().name)) {
                        LineMoneyContext.setLocalLanguagePair(Config.getAvailableLang(1));
                    } else {
                        LineMoneyContext.setLocalLanguagePair(Config.getAvailableLang(0));
                    }
                    activity.startActivityWithTaskCleared(MainActivity.class);
                }
            }
        });
    }
}
